
var roi = FJShp;
// 绘制矢量边界
function addMyLayer(edge) {
  var empty = ee.Image().toByte();
  var outline = empty.paint({
    featureCollection:edge,
    color: 0,
    width: 1
  });
  Map.addLayer(outline, {palette: "ff0000"}, "Outline");
}

addMyLayer(roi);

function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
  var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      .and(qa.bitwiseAnd(cirrusBitMask).eq(0));

  return image.updateMask(mask).divide(10000);
}

var dataset = ee.ImageCollection('COPERNICUS/S2_SR_HARMONIZED')
                  .filterDate('2022-09-20', '2022-09-30')
                  // Pre-filter to get less cloudy granules.
                  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE',15))
                  .filterBounds(roi)
                  .map(maskS2clouds);

var visualization = {
  min: 0.0,
  max: 0.3,
  bands: ['B4', 'B3', 'B2'],
};
print(dataset);
var composite = dataset.mean();
Map.addLayer(composite, visualization, 'RGB');
Map.centerObject(roi, 10);

// 导出影像到 Google Drive
Export.image.toDrive({
  image: composite,            // 要导出的影像
  description: 'Sentinel2_Export',  // 导出的任务名称
  folder: 'GEE_Exports',       // Google Drive 文件夹名称
  fileNamePrefix: 'Sentinel2_Composite',  // 文件名前缀
  region: roi,                 // 导出的区域
  scale: 10,                   // 空间分辨率（单位：米）
  crs: 'EPSG:4326',            // 坐标系（WGS 84）
  maxPixels: 1e9              // 最大像素数
});